package com.ailatrieuphu;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Message {
	ByteArrayInputStream aaaaaaaaaaaaa;
	public DataInputStream aaaaaaaaaa;
	ByteArrayOutputStream aaaaaaaaa;
	DataOutputStream aaaaaaa;
	private short aaaaaaaaaaaaaaaaaaaa;
	private boolean aaaaaaaaaaaaaaa;
	private int aaaaaaaaaaaaaa = 500;

	public void desTroy() {
		if (aaaaaaaaaaaaa != null)
			try {
				aaaaaaaaaaaaa.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		if (aaaaaaaaaa != null)
			try {
				aaaaaaaaaa.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		if (aaaaaaaaa != null)
			try {
				aaaaaaaaa.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		if (aaaaaaa != null)
			try {
				aaaaaaa.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	public boolean isCorrect() {
		return aaaaaaaaaaaaaaa;
	}

	// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// //Read data
	public Message(byte[] data) {
		aaaaaaaaaaaaaaa = true;
		aaaaaaaaaaaaa = new ByteArrayInputStream(data);
		aaaaaaaaaa = new DataInputStream(aaaaaaaaaaaaa);
		aaaaaaaaa = null;
		aaaaaaa = null;
		aaaaaaaaaaaaaa = 500;
		try {
			aaaaaaaaaaaaaaaaaaaa = aaaaaaaaaa.readShort();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Can't read Command");
			aaaaaaaaaaaaaaa = false;
		}
	}

	public boolean readBoolean() {
		try {
			return aaaaaaaaaa.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return false;
		}
	}

	public byte readByte() {
		try {
			return aaaaaaaaaa.readByte();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public short readShort() {
		try {
			return aaaaaaaaaa.readShort();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public int readInt() {
		try {
			return aaaaaaaaaa.readInt();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public long readLong() {
		try {
			return aaaaaaaaaa.readLong();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public float readFloat() {
		try {
			return aaaaaaaaaa.readFloat();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public double readDouble() {
		try {
			return aaaaaaaaaa.readDouble();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public char readChar() {
		try {
			return aaaaaaaaaa.readChar();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return 0;
		}
	}

	public String readString() {
		try {
			return aaaaaaaaaa.readUTF();
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return null;
		}
	}

	public byte[] readByteArray() {
		try {
			int n = aaaaaaaaaa.readInt();
			if (n == 0)
				return null;
			byte[] data = new byte[n];
			for (int i = 0; i < n; i++)
				data[i] = aaaaaaaaaa.readByte();
			return data;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public short[] readShortArray() {
		try {
			short n = aaaaaaaaaa.readShort();
			if (n == 0)
				return null;
			short[] data = new short[n];
			for (int i = 0; i < n; i++)
				data[i] = aaaaaaaaaa.readShort();
			return data;
		} catch (IOException e) {
			e.printStackTrace();
			aaaaaaaaaaaaaaa = false;
			return null;
		}
	}

	// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// //Send data
	public Message(short CMD) {
		aaaaaaaaaaaaaaa = true;
		aaaaaaaaaaaaa = null;
		aaaaaaaaaa = null;
		aaaaaaaaa = new ByteArrayOutputStream();
		aaaaaaa = new DataOutputStream(aaaaaaaaa);
		aaaaaaaaaaaaaa = 500;
		aaaaaaaaaaaaaaaaaaaa = CMD;
		try {
			aaaaaaa.writeShort(CMD);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeBoolean(boolean b) {
		try {
			aaaaaaa.writeBoolean(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeByte(byte b) {
		try {
			aaaaaaa.writeByte(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeshort(short n) {
		try {
			aaaaaaa.writeShort(n);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeInt(int n) {
		try {
			aaaaaaa.writeInt(n);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeLong(long l) {
		try {
			aaaaaaa.writeLong(l);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeFloat(float f) {
		try {
			aaaaaaa.writeFloat(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeDouble(double d) {
		try {
			aaaaaaa.writeDouble(d);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeChar(char c) {
		try {
			aaaaaaa.writeChar(c);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeString(String s) {
		try {
			if (s == null)
				s = "";
			aaaaaaa.writeUTF(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeByteArray(byte[] data) {
		try {
			if (data == null)
				aaaaaaa.writeInt(0);
			else {
				aaaaaaa.writeInt(data.length);
				aaaaaaa.write(data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeByteArray(byte[] data, int offset, int length) {
		try {
			if (data == null)
				aaaaaaa.writeInt(0);
			else {
				aaaaaaa.writeInt(length);
				aaaaaaa.write(data, offset, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public short getCMD() {
		return aaaaaaaaaaaaaaaaaaaa;
	}

	public byte[] getByteArrayBuffer() {
		return aaaaaaaaa.toByteArray();
	}

	public int getSleep() {
		return aaaaaaaaaaaaaa;
	}

	public void setSleep(int sleep) {
		this.aaaaaaaaaaaaaa = sleep;
	}
}
